home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / dns / inet.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  2.0 KB  |  75 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Generic Internet address helper functions.'''
  5. import socket
  6. import dns.ipv4 as dns
  7. import dns.ipv6 as dns
  8. AF_INET = socket.AF_INET
  9.  
  10. try:
  11.     AF_INET6 = socket.AF_INET6
  12. except AttributeError:
  13.     AF_INET6 = 9999
  14.  
  15.  
  16. def inet_pton(family, text):
  17.     '''Convert the textual form of a network address into its binary form.
  18.  
  19.     @param family: the address family
  20.     @type family: int
  21.     @param text: the textual address
  22.     @type text: string
  23.     @raises NotImplementedError: the address family specified is not
  24.     implemented.
  25.     @rtype: string
  26.     '''
  27.     if family == AF_INET:
  28.         return dns.ipv4.inet_aton(text)
  29.     elif family == AF_INET6:
  30.         return dns.ipv6.inet_aton(text)
  31.     else:
  32.         raise NotImplementedError
  33.  
  34.  
  35. def inet_ntop(family, address):
  36.     '''Convert the binary form of a network address into its textual form.
  37.  
  38.     @param family: the address family
  39.     @type family: int
  40.     @param address: the binary address
  41.     @type address: string
  42.     @raises NotImplementedError: the address family specified is not
  43.     implemented.
  44.     @rtype: string
  45.     '''
  46.     if family == AF_INET:
  47.         return dns.ipv4.inet_ntoa(address)
  48.     elif family == AF_INET6:
  49.         return dns.ipv6.inet_ntoa(address)
  50.     else:
  51.         raise NotImplementedError
  52.  
  53.  
  54. def af_for_address(text):
  55.     '''Determine the address family of a textual-form network address.
  56.  
  57.     @param text: the textual address
  58.     @type text: string
  59.     @raises ValueError: the address family cannot be determined from the input.
  60.     @rtype int
  61.     '''
  62.     
  63.     try:
  64.         junk = dns.ipv4.inet_aton(text)
  65.         return AF_INET
  66.     except:
  67.         
  68.         try:
  69.             junk = dns.ipv6.inet_aton(text)
  70.             return AF_INET6
  71.         raise ValueError
  72.  
  73.  
  74.  
  75.